// 价格格式化函数
function formatPrice(price, decimalPlaces = 2) {
  const fixedPrice = price.toFixed(decimalPlaces);  // 保留两位小数
  const parts = fixedPrice.split('.');
  const integerPart = parts[0].replace(/\B(?=(\d{3})+(?!\d))/, ',');  // 每三位加逗号
  return parts.length > 1 ? `${integerPart}.${parts[1]}` : integerPart;
}

// 计算折扣后价格的函数
function calculateDiscountedPrice(originalPrice, discountRate) {
  return originalPrice * (1 - discountRate);
}

// 计算商品总价的函数
function calculateTotalPrice(products) {
  return products.reduce((total, product) => {
    return total + product.price * product.quantity;
  }, 0);
}

// 查找目标价格所在价格区间的函数
function findPriceRange(priceRanges, targetPrice) {
  for (let i = 0; i < priceRanges.length; i++) {
    const [min, max] = priceRanges[i];
    if (targetPrice >= min && targetPrice <= max) {
      return priceRanges[i];
    }
  }
  return null;
}

// 测试价格格式化
const price = 1234567.89;
console.log(`格式化后的价格: ${formatPrice(price)}`);

// 测试折扣计算
const originalPrice = 100;
const discountRate = 0.2;
console.log(`折扣后的价格: ${calculateDiscountedPrice(originalPrice, discountRate)}`);

// 测试商品总价计算
const products = [
  { price: 10, quantity: 2 },
  { price: 20, quantity: 3 },
  { price: 5, quantity: 4 }
];
console.log(`商品总价: ${calculateTotalPrice(products)}`);

// 测试价格区间查找
const priceRanges = [
  [0, 100],
  [101, 200],
  [201, 300]
];
const targetPrice = 150;
console.log(`目标价格所在区间: ${findPriceRange(priceRanges, targetPrice)}`);
